<script>
	import {getOpenIDAction,LocationResolution,getMemberInfoAction,getAddAction,RedisAdd,generateTouristCode,GetChengXu} from '@/api/global.js';
	import {getCurrentTimestamp} from '@/static/js/DateUtil.js'
	import {createUniqueString} from '@/static/js/GeneralUtil.js'
	export default {
		data() {
			return {
				memberInfo: {
					//用户表
					Member_SerialNumber: "",
					Nick: "游客",
					Image: "/static/icon/tourist.png",
					Member_Name: "",
					Mobile: "",
					Head_URL: "",
					Open_ID: "",
					Sales_ID: "",
					Login_authorization: 0,
					Rights_Type: 0,
					Dealer_Authority: "",
					Employee_Authority: 0,
					// App_ID: uni.getAccountInfoSync().miniProgram.appId,
					App_ID: 'wx9b74a42a123612c9',
					Appoint_Member_Type: 0,
					Enterprise_Info_SerialNumber: "91230102MA1BJQH87C",
					Children: {
						member_account_info: [{
							Member_Account_SerialNumber: "",
							Member_SerialNumber: "",
							Account_Type: 0,
							Prepayment: 0,
							Prepayment_Sales: 0,
							Prepayment_Balance: 0,
							Gift_Amount: 0,
							Gift_Sales: 0,
							Gift_Balance: 0,
							Sales_Point: 0,
							Sales_Point_Sales: 0,
							Sales_Point_Balance: 0,
							Gift_Point: 0,
							Gift_Point_Sales: 0,
							Gift_Point_Balance: 0,
						}],
					},
				},
				position: {},
			};
		},
		globalData: { //全局变量
			// Domain_Name: 'https://3451m7y168.goho.co/svxklj',
			// Domain_Name: 'http://192.168.1.90:8080/svxklj',
			Domain_Name: 'https://service.xiaokanglongjiang.com/svxklj',
			Domain_Url: 'https://marketing.xiaokanglongjiang.com/dist/h5XKLJ/index.html',
			// Domain_Url:'http://localhost:8081/dist/h5XKLJ/',
			Domain_WZYM: 'https://marketing.xiaokanglongjiang.com/dist/h5XKLJ/',
			// Domain_WZYM: 'http://localhost:8080',
			Domain: 'https://marketing.xiaokanglongjiang.com',
			// Domain_IMG: "https://digital.essc.top/dist/imgxklj", //图片域名
			// Domain_Name: 'https://marketingservice.ediblefungus.cn/svxklj',
			// Domain_IMG: "https://3451m7y168.goho.co/svxklj/img", 
			// Domain_IMG: '/static/img',
			ThemeColors: 'success',
			// Domain_IMG: 'https://image01.xiaokanglongjiang.com/dist/imgxklj', //图片域名
			Domain_IMG: 'https://marketing.xiaokanglongjiang.com/dist/imgxklj', //图片域名
			// Domain_ICON:'https://marketing.ediblefungus.cn/dist/iconxklj',//图片域名
			Domain_ICON: "https://image01.xiaokanglongjiang.com/dist/iconxklj", //图标
			// Domain_ICON: "https://digital.essc.top/dist/iconxklj", //图标
			Domain_dead: "/static/icon", // 测试
			Open_ID: "", //用户Open_ID
			Tourist_Code: "", //游客 ID
			// App_ID: "wxd96a1f0fbc125263",
			// App_ID: uni.getAccountInfoSync().miniProgram.appId, //小程序appid
			App_ID: 'wx9b74a42a123612c9', //小程序appid
			Enterprise_Info_SerialNumber: "91230102MA1BJQH87C", //企业编号
			memberInfo: {}, //用户信息
			createUniqueString: '',
			isPC:true
		},
		onLaunch: function() {
			// #ifdef MP-WEIXIN
			let ls_App_ID = this.$scope.globalData.App_ID;
			let ls_Enterprise_Info_SerialNumber = this.$scope.globalData.Enterprise_Info_SerialNumber
			this.getMyLocation((location) => {
				//获取Open_ID
				this.onLogin((loginRes) => {
					this.getOpenID(loginRes.code, ls_App_ID, (Res) => {
						uni.setStorageSync('hmgx_open_id', Res.Open_ID)
						this.RedisAdd(Res.Open_ID + '_address', JSON.stringify(location))
						uni.setStorageSync("ShareUrl",`/pages/index/none?openid=${Res.Open_ID}`);
						this.$scope.globalData.Open_ID = Res.Open_ID
						this.$scope.globalData.createUniqueString = Res.Open_ID

						//获取用户信息
						this.getMemberInfo(Res.Open_ID, ls_App_ID,ls_Enterprise_Info_SerialNumber, ({Data}) => {
							// uni.hideLoading();
							// uni.reLaunch({
							// 	url: '/pages/index/index'
							// })
							// 判断是否有用户信息，无则提醒注册，有则直接保存
							if (Data.length > 0) { //存储当前用户信息
								this.$scope.globalData.memberInfo = Data[0];
							} else {
								// 添加默认用户信息
								this.memberInfo.Open_ID = Res.Open_ID;
								this.getAddAction({
									Data: JSON.stringify({member_info: [this.memberInfo]}),Open_ID: Res.Open_ID
								}, ({Status,Data}) => {
									if (Status == "success") {
										this.$scope.globalData.memberInfo =this.memberInfo
										uni.showModal({
											title: "提示",
											content: "是否完善个人信息",
											success: (res) => {
												if (res.confirm) { // 成功跳转修改信息页
													uni.navigateTo({url: '/pages/my/information?Member_SerialNumber=' +Data})
												}
											}
										})
									}
								})
							}
						})
					})
				})
			});
			// #endif
			// #ifdef H5
			//调试用户
			// this.getMemberInfo("ojEAP4-rf7FJNdyMzEN5xcpXmjSk", "wx9b74a42a123612c9","91230102MA1BJQH87C", ({Data}) => {
			// 	// 判断是否有用户信息，无则提醒注册，有则直接保存
			// 	if (Data.length > 0) { //存储当前用户信息
			// 		this.globalData.memberInfo = Data[0];
			// 	}
			// })
			// #endif
			this.GetChengXu()
		},
		methods: {
			// 判断当前是不是小程序
			async GetChengXu() {
				let status = await GetChengXu();
				if (status) {
					this.generateTouristCode()
				}
			},
			// 获取游客code码
			async generateTouristCode() {
				let res = await generateTouristCode();
				if (res.Status == 'success') {
					this.globalData.Tourist_Code = res.Data
				}
			},
			onLogin(callback) { //获取用户登录Code码
				uni.login({
					provider: "weixin",
					success: function(loginRes) {
						callback(loginRes)
					}
				})
			},
			async getOpenID(Code, App_ID, callback) { //获取用户Open_ID
				callback(await getOpenIDAction({
					Code,
					App_ID
				}))
			},
			async getMemberInfo(Open_ID, App_ID, Enterprise_Info_SerialNumber, callback) { //查询用户信息
				callback(await getMemberInfoAction({
					Open_ID,
					App_ID,
					Enterprise_Info_SerialNumber
				}))
			},
			async RedisAdd(Key, Main) { //redis
				let res = await RedisAdd({
					Key,
					Main
				})
			},
			getMyLocation(callback) { //获取缓存中用户地址
				let AddressLocation = uni.getStorageSync("AddressLocation")
				if (AddressLocation && AddressLocation.expires_in > getCurrentTimestamp()) {
					callback(AddressLocation.data)
				} else {
					this.getLocation((data) => {
						uni.setStorageSync("AddressLocation", {
							data,
							expires_in: getCurrentTimestamp() + 1800
						})
						callback(data)
					})
				}
			},
			getLocation(callback) { //获取用户当前地址
				uni.getLocation({
					type: 'gcj02',
					success: async ({
						latitude,
						longitude
					}) => {
						const res = await LocationResolution({
							latitude,
							longitude,
							Open_ID: this.$scope.globalData.Open_ID
						})

						callback({
							adcode: res.Data.ad_info.adcode,
							nation_code: res.Data.ad_info.nation_code,
							nation: res.Data.ad_info.nation,
							longitude: res.Data.location.lng,
							latitude: res.Data.location.lat,
							province: res.Data.address_component.province,
							city: res.Data.address_component.city,
							district: res.Data.address_component.district,
							street: res.Data.address_component.street,
							address: res.Data.address,
						})
					}
				})
			},
			async getAddAction(Data, callback) {
				callback(await getAddAction(Data));
			},
			watch(method, istr) {
				var obj = this.globalData
				Object.defineProperty(obj, istr, {
					configurable: true,
					enumerable: true,
					set: function(value) {
						this._consumerGoodsStatus = value
						method(value)
					},
					get: function(value) {
						return this._consumerGoodsStatus
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@media screen and (min-width:1200px) {
		uni-page-body {
			width: 1200px;
			margin: 0 auto;
		}
	}

	/*每个页面公共css */
	@font-face {
		font-family: iconfont;
		src: url('static/css/iconfont.ttf');
	}

	::-webkit-scrollbar {
		display: none;
		/* 		width:0 !important;
		height:0 !important;
		-webkit-appearance: none;
		background: transparent; */
	}

	@import "static/css/free.css";
</style>